package ar.edu.caece.compiler.grammar.factories;

import ar.edu.caece.compiler.grammar.AbstractTokenVisitorFactory;
import ar.edu.caece.compiler.grammar.GrammarSymbol;
import ar.edu.caece.compiler.grammar.TokenVisitorInterface;
import ar.edu.caece.compiler.grammar.nonterminals.EXPR2A;
import ar.edu.caece.compiler.grammar.nonterminals.EXPR2A1;
import ar.edu.caece.compiler.grammar.nonterminals.EXPR3;
import ar.edu.caece.compiler.grammar.nonterminals.OP2;
import ar.edu.caece.compiler.grammar.terminals.GrammarTerminal;

public class EXPR2A1Factory extends AbstractTokenVisitorFactory {

	private static EXPR2A1Factory INSTANCE;
	
	private OP2Factory op2Factory;
	private EXPR3Factory expr3Factory;
	private EXPR2AFactory expr2aFactory;
	private TokenVisitorInterface parseOP2;
	private TokenVisitorInterface parseEXPR3;
	private TokenVisitorInterface parseEXPR2A;
	
	public static EXPR2A1Factory getInstance() throws Exception {
		if(INSTANCE==null) INSTANCE = new EXPR2A1Factory();
		return INSTANCE;
	}
	
	private Initializer initializer = new Initializer() {
		
		@Override
		public void init() throws Exception {
			initializer = NoOperationInitializer.getInstance();
			
			op2Factory = OP2Factory.getInstance();
			expr3Factory = EXPR3Factory.getInstance();
			expr2aFactory = EXPR2AFactory.getInstance();
			parseEXPR2A  = expr2aFactory.makeVisitor();
			parseEXPR3 = expr3Factory.makeVisitor();
			parseOP2 = op2Factory.makeVisitor();
		}
	};
	
	
	private EXPR2A1Factory() throws Exception {
	}
	
	@Override
	public TokenVisitorInterface makeChainedVisitor(
			final TokenVisitorInterface siguiente) throws Exception {
		initializer.init();

		return new TokenVisitorInterface() {

			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador)
					throws Exception {
				GrammarSymbol result = llamador.execute(op2Factory.makeChainedVisitor(siguiente));
				if(result instanceof OP2) {
					return new EXPR2A1 ((OP2) result, (EXPR3) nextToken().execute(parseEXPR3),(EXPR2A) nextToken().execute(parseEXPR2A)); 
				} 
				else {
					return result;
				}
			}
			
		};
	
	}
	@Override
	public TokenVisitorInterface makeVisitor() throws Exception {
		initializer.init();

		return new TokenVisitorInterface() {

			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador) throws Exception {
				return new EXPR2A1 ((OP2) llamador.execute(parseOP2), (EXPR3) nextToken().execute(parseEXPR3),(EXPR2A) nextToken().execute(parseEXPR2A));
			}
		};
	}
	
}
